from django.db import models
from django.contrib.auth.models import AbstractUser
import re

from django.core import validators
from django.utils.deconstruct import deconstructible

"""
用户表设计:
    用户名
    邮箱
    手机号
    nickname(昵称)
    Gender 性别
    生日

"""


@deconstructible
class UnicodePhoneNumberValidator(validators.RegexValidator):
    """
    验证手机号
    """
    regex = r'^[0-9]{11}$'
    message = (
        '请输入手机号 '
        'numbers,0-9'
    )
    flags = 0


class User(AbstractUser):
    # 重写用户表

    GENDER_CHOICES = (
        (0, "男"),
        (1, "女"),
        (2, "保密")
    )
    phone_number_validator = UnicodePhoneNumberValidator()

    nickname = models.CharField(max_length=32, default="", verbose_name="昵称")
    gender = models.SmallIntegerField(choices=GENDER_CHOICES, default=2, verbose_name="性别")
    phone_number = models.CharField(
        max_length=11,
        unique=True,
        help_text='手机号码必须为11位数字',
        validators=[phone_number_validator],
        error_messages={'unique': "手机号码已存在"},
        verbose_name="手机号码",
    )
    birthday = models.DateField(verbose_name="生日", default="2000-01-01")

    class Meta:
        db_table = 'user'
        verbose_name = "用户"
        verbose_name_plural = verbose_name
